//
// Created by QT on 2023/11/8.
//


#include "UserMain.h"

#include "adc.h"
#include "opamp.h"
#include "tim.h"
#include "dac.h"
#include "comp.h"

//#include "arm_math.h"


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "UserMain.h"




uint32_t Ia = 0, Ib = 0, Ic = 0;

uint32_t DAC_wave[100] = {0x0800, 0x0881, 0x0901, 0x0980, 0x09FD, 0x0A79, 0x0AF2, 0x0B68, 0x0BDA, 0x0C49,
                          0x0CB3, 0x0D19, 0x0D79, 0x0DD4, 0x0E29, 0x0E78, 0x0EC0, 0x0F02, 0x0F3C, 0x0F6F,
                          0x0F9B, 0x0FBF, 0x0FDB, 0x0FEF, 0x0FFB, 0x0FFF, 0x0FFB, 0x0FEF, 0x0FDB, 0x0FBF,
                          0x0F9B, 0x0F6F, 0x0F3C, 0x0F02, 0x0EC0, 0x0E78, 0x0E29, 0x0DD4, 0x0D79, 0x0D19,
                          0x0CB3, 0x0C49, 0x0BDA, 0x0B68, 0x0AF2, 0x0A79, 0x09FD, 0x0980, 0x0901, 0x0881,
                          0x0800, 0x077F, 0x06FF, 0x0680, 0x0603, 0x0587, 0x050E, 0x0498, 0x0426, 0x03B7,
                          0x034D, 0x02E7, 0x0287, 0x022C, 0x01D7, 0x0188, 0x0140, 0x00FE, 0x00C4, 0x0091,
                          0x0065, 0x0041, 0x0025, 0x0011, 0x0005, 0x0001, 0x0005, 0x0011, 0x0025, 0x0041,
                          0x0065, 0x0091, 0x00C4, 0x00FE, 0x0140, 0x0188, 0x01D7, 0x022C, 0x0287, 0x02E7,
                          0x034D, 0x03B7, 0x0426, 0x0498, 0x050E, 0x0587, 0x0603, 0x0680, 0x06FF, 0x077F};



void UserMain()
{
    printf("Entry UserMain()\n");

//    HAL_OPAMPEx_SelfCalibrateAll(&hopamp1, &hopamp2, &hopamp3);
//    HAL_OPAMP_Start(&hopamp1);
//    HAL_OPAMP_Start(&hopamp2);
//    HAL_OPAMP_Start(&hopamp3);
//
//
//    HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED);
//    HAL_ADCEx_Calibration_Start(&hadc2, ADC_SINGLE_ENDED);
//
//
//    HAL_ADC_Start_IT(&hadc1);
//
////    HAL_ADCEx_InjectedStart_IT(&hadc1);
////    HAL_ADCEx_InjectedStart_IT(&hadc2);
//
//
//    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 2000);
//    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, 5000);
//    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, 4000);
//
//    HAL_TIM_Base_Start(&htim1);
//
//    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
//    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
//    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
//    HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);
//    HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2);
//    HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_3);
//
//    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
//    HAL_DAC_Start(&hdac3, DAC_CHANNEL_1);
//    HAL_DAC_SetValue(&hdac3, DAC_CHANNEL_1, DAC_ALIGN_12B_R, 3000);
//    HAL_COMP_Start(&hcomp1);
//
//    HAL_ADCEx_InjectedStart_IT(&hadc1);
//    HAL_ADCEx_InjectedStart(&hadc2);
//
//
//    HAL_DAC_Start(&hdac1, DAC_CHANNEL_1);


    //arm_sin_f32(30);

    printf("Leave UserMain()\n");
}



/**
  * @brief  Conversion complete callback in non-blocking mode.
  * @param hadc ADC handle
  * @retval None
  */
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
    if (hadc->Instance == ADC1)
    {

    }
}


/**
  * @brief  Injected conversion complete callback in non-blocking mode.
  * @param hadc ADC handle
  * @retval None
  */
void HAL_ADCEx_InjectedConvCpltCallback(ADC_HandleTypeDef *hadc)
{
    if (hadc->Instance == ADC1)
    {
        Ia = HAL_ADCEx_InjectedGetValue(hadc, ADC_INJECTED_RANK_1);
        Ib = HAL_ADCEx_InjectedGetValue(hadc, ADC_INJECTED_RANK_2);

    }

    if (hadc->Instance == ADC2)
    {
        Ic = HAL_ADCEx_InjectedGetValue(hadc, ADC_INJECTED_RANK_1);
    }

//    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_4);

    static int cnt = 0;
    HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, DAC_wave[cnt++]);
    if (cnt >= 100) cnt = 0;

    //printf("Ia:%lu, Ib:%lu, Ic:%lu\n", Ia, Ib, Ic);
}


